﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Company;
using System.Web.Security;
using Avatar.Common;
using Avatar.MembershipService;
using AvatarHelper;

public partial class Company_JobManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Membership.GetUser() != null)
            {
                var _jobs = JobManager.GetJobList(Membership.GetUser().UserName,1);
                ListView1.DataSource = _jobs;
                ListView1.DataBind();
            }
            using(var _adc=DBHelper.GetDataContext())
            {
                ddl_Sex.DataSource = _adc.Sex.ToList();
                ddl_Sex.DataTextField = "name";
                ddl_Sex.DataValueField = "id";
                ddl_Sex.DataBind();
            }
            if (Request.QueryString["ID"] != null)
            {
                Guid _id = new Guid(Request.QueryString["ID"].ToString());
                var _job = JobManager.GetJob(_id);
                if (_job != null)
                {
                    btn_AddNew.Visible = false;
                    panel_Edit.Visible = true;

                    txt_Title.Text = _job.Title;
                    ddl_Industry.SelectedValue = _job.IndustryID.ToString();
                    ddl_Position.SelectedValue = _job.PositionID.ToString();
                    txt_PositionName.Text = _job.PositionName;
                    rbl_JobType.SelectedValue = _job.JobTypeID.ToString();
                    txt_Number.Text = _job.Number == 0 ? "" : _job.Number.ToString();
                    ddl_Sex.SelectedValue = _job.SexID.ToString();
                    ddl_Diploma.SelectedValue = _job.DiplomaID.ToString();
                    ddl_WorkingAge.SelectedValue = _job.WorkingAgeID.ToString();
                    ddl_Language.SelectedValue = _job.LanguageID.ToString();
                    ddl_LanguageAbility.SelectedValue = _job.LanguageAbilityID.ToString();
                    ddl_Location.City = _job.LocationID;
                    ddl_Salary.SelectedValue = _job.SalaryID.ToString();
                    ddl_Period.SelectedValue = _job.PeriodID.ToString();
                    txt_Description.Text = _job.Detail;
                }
            }
        }
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        try
        {
            var _user = LoginManager.GetCurrentUser();
            if (_user != null)
            {
                try
                {
                    if (!string.IsNullOrEmpty(txt_Number.Text))
                        Convert.ToInt32(txt_Number.Text);
                }
                catch (Exception)
                {
                    throw new Exception("招聘人数只可填数字");
                }
                Guid _id = Request.QueryString["ID"] == null ? Guid.Empty : new Guid(Request.QueryString["ID"].ToString());

                JobManager.JobSave(txt_Title.Text,
                    Convert.ToInt32(ddl_Industry.SelectedValue),
                    _id,
                    Convert.ToInt32(ddl_Position.SelectedValue),
                    txt_PositionName.Text,
                    Convert.ToInt32(rbl_JobType.SelectedValue),
                    _user.UserName,
                    txt_Description.Text,
                    Convert.ToInt32(ddl_Diploma.SelectedValue),
                    Convert.ToInt32(ddl_Language.SelectedValue),
                    string.IsNullOrEmpty(ddl_LanguageAbility.SelectedValue) ? 0 : Convert.ToInt32(ddl_LanguageAbility.SelectedValue),
                    ddl_Location.City,
                    string.IsNullOrEmpty(txt_Number.Text) ? 0 : Convert.ToInt32(txt_Number.Text),
                    Convert.ToInt32(ddl_Period.SelectedValue),
                    Convert.ToInt32(ddl_Salary.SelectedValue),
                    string.IsNullOrEmpty(ddl_Sex.SelectedValue) ? 0 : Convert.ToInt32(ddl_Sex.SelectedValue),
                    Convert.ToInt32(ddl_WorkingAge.SelectedValue));

                Response.Redirect("Company_JobManage.aspx");

                btn_AddNew.Visible = true;
                panel_Edit.Visible = false;
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
        btn_AddNew.Visible = true;
        Response.Redirect("./Company_JobManage.aspx");
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            Guid _id = Request.QueryString["ID"] == null ? Guid.Empty : new Guid(Request.QueryString["ID"].ToString());
            using (var _adc = DBHelper.GetDataContext())
            {
                var _job = _adc.Job.FirstOrDefault(j=>j.ID==_id);
                if (_job != null)
                {
                    _adc.Job.DeleteOnSubmit(_job);
                    _adc.SubmitChanges();
                }
                Response.Redirect("./company_jobmanage.aspx");
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_AddNew_Click(object sender, EventArgs e)
    {
        btn_AddNew.Visible = false;
        panel_Edit.Visible = true;
    }
    protected string DateTimeFormat(DateTime _date)
    {
        return Helper.DateTimeStringFormat(_date);
    }
    protected void ddl_JobStatusList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Membership.GetUser() != null)
        {
            var _jobs = JobManager.GetJobList(Membership.GetUser().UserName, Convert.ToInt32(ddl_JobStatusList.SelectedValue));
            ListView1.DataSource = _jobs;
            ListView1.DataBind();
        }
    }
}
